//
//  ZJPageTitleView.m
//  ScrollPageView
//
//  Created by kepuna on 2017/4/12.
//  Copyright © 2017年 kepuna. All rights reserved.
//

#import "ZJPageTitleView.h"

#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kBaseWidth  ScreenWidth / 375.0
#define kBaseHeight ScreenHeight / 667.0

static const CGFloat kScrollBarHeight = 2;
static const CGFloat kBottomLineHeight = 0.5;

@interface ZJPageTitleView ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *buttons; // 存放button的数组
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *scrollBarView;
@property (nonatomic, strong) UIView *bottomLineView;
@property (nonatomic, assign) CGFloat titleWidth;
@property (nonatomic, assign) CGFloat titleHeight;
@property (nonatomic, strong) UIButton *oldBtn;

@property (nonatomic, assign) NSInteger currentIndex;


@end

@implementation ZJPageTitleView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.titles = titles;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 添加scrollView
    [self addSubview:self.scrollView];
    // 创建label数组
    [self setupTitleLabels];
    // 创建底部的线和滚动的bar
    [self setupScrollLineAndBottomLine];
    // 设置默认选中第一个menu
    [self selectedDefault];
}

- (void)selectedDefault {
    
    UIButton *firstBtn = self.buttons.firstObject;
    firstBtn.selected = YES;
    self.oldBtn = firstBtn;
}

- (void)setupScrollLineAndBottomLine {
    
    [self addSubview:self.bottomLineView];
    [self addSubview:self.scrollBarView];

}

- (void)setupTitleLabels {
    
    self.titleWidth = ScreenWidth / self.titles.count;
    self.titleHeight = self.bounds.size.height - kScrollBarHeight * kBaseHeight;
    CGFloat titleY = 0;
    NSMutableArray *tempArray = [NSMutableArray array];
    [self.titles enumerateObjectsUsingBlock:^(NSString  *title, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CGFloat rgb = 153.0 / 255.0;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:0/255.0 green:191.0/255.0 blue:143.0/255.0 alpha:1.0] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(s_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:16 * kBaseHeight];
        button.tag = idx;
        CGFloat titleX = self.titleWidth * idx;
        button.frame = CGRectMake(titleX, titleY, self.titleWidth, self.titleHeight);
        [self.scrollView addSubview:button];
        [tempArray addObject:button];
    }];
    self.buttons = tempArray.copy;
}

- (void)s_buttonClick:(UIButton *)sender {
//    NSLog(@"--------sender.tab == %zd, currendix = %zd",sender.tag,self.currentIndex);
    if (sender.tag == self.currentIndex) {
        return;
    }
    [self.buttons enumerateObjectsUsingBlock:^(UIButton  *btn, NSUInteger idx, BOOL * _Nonnull stop) {
         btn.selected = (btn.tag == sender.tag) ? YES : NO;
    }];

    self.currentIndex = sender.tag;
    [UIView animateWithDuration:0.25 animations:^{
        CGFloat offsetX = sender.tag * self.titleWidth;
        self.scrollBarView.transform = CGAffineTransformMakeTranslation(offsetX, 0);
    }];
    
    if ([self.delegate respondsToSelector:@selector(pageTitleView:clickIndex:)]) {
        [self.delegate pageTitleView:self clickIndex:self.currentIndex];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ZJPageScrollViewTitleClickNotification" object:nil];
    }
}

- (void)setTitleWithTargetIndex:(NSInteger)targetIndex {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ZJPageScrollViewTitleClickNotification" object:nil];
    
//    NSLog(@"---------targetIndex == %zd",targetIndex);
    [UIView animateWithDuration:0.25 animations:^{
        self.scrollBarView.transform = CGAffineTransformMakeTranslation(targetIndex * self.titleWidth, 0);
    } completion:^(BOOL finished) {
        [self.buttons enumerateObjectsUsingBlock:^(UIButton  *button, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == targetIndex) {
                button.selected = YES;
            }else {
                button.selected = NO;
            }
        }];
        self.currentIndex = targetIndex;
    }];
    

}

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.frame = self.bounds;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollsToTop = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (UIView *)scrollBarView {
    if (_scrollBarView == nil) {
        _scrollBarView = [[UIView alloc]init];
        _scrollBarView.backgroundColor = [UIColor colorWithRed:0/255.0 green:191.0/255.0 blue:143.0/255.0 alpha:1.0];
        _scrollBarView.frame = CGRectMake(0,self.titleHeight,self.titleWidth , kScrollBarHeight * kBaseHeight);
    }
    return _scrollBarView;
}

- (UIView *)bottomLineView {
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc]init];
        CGFloat rgb = 229.0 / 255.0;
        _bottomLineView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
        _bottomLineView.frame = CGRectMake(0, self.bounds.size.height - kBottomLineHeight, ScreenWidth, kBottomLineHeight);
    }
    return _bottomLineView;
}

- (NSArray *)buttons {
    if (_buttons == nil) {
        _buttons = [[NSArray alloc]init];
    }
    return _buttons;
}

+ (CGFloat)height {
    return 44 * kBaseHeight;
}

@end
